package de.koller.aframe.util.filter;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.util.ColorIntUtil;

public class DownSample {
	
	/** Downsamples from 6-times the size */
	public static void useHigh( RasterManager big, RasterManager out ) {
		if( out.getWidth() * 6 != big.getWidth() || out.getHeight() * 6 != big.getHeight() )
			throw new OceanException();
		
		for( int x = 0; x < out.getWidth(); x++ )
			for( int y = 0; y < out.getHeight(); y++ ) {
				
				int a = 0;
				int r = 0;
				int g = 0;
				int b = 0;
				
				for( int xi = 0; xi < 6; xi++ )
					for( int yi = 0; yi < 6; yi++ ) {
						a += ColorIntUtil.getAValue( big.colorAt( x*6 + xi, y*6 + yi ) );
						r += ColorIntUtil.getRValue( big.colorAt( x*6 + xi, y*6 + yi ) );
						g += ColorIntUtil.getGValue( big.colorAt( x*6 + xi, y*6 + yi ) );
						b += ColorIntUtil.getBValue( big.colorAt( x*6 + xi, y*6 + yi ) );
					}
				
				a /= 36; r /= 36; g /= 36; b /= 36;
				out.setPixel( ColorIntUtil.getIntColor( a, r, g, b ), x, y );		
			}
		
	}
	
	public static void use( RasterManager big, RasterManager out ) {
		if( out.getWidth() * 3 != big.getWidth() || out.getHeight() * 3 != big.getHeight() )
			throw new OceanException();
		
		for( int x = 0; x < out.getWidth(); x++ )
			for( int y = 0; y < out.getHeight(); y++ ) {
				
				int a = 0;
				int r = 0;
				int g = 0;
				int b = 0;
				
				for( int xi = 0; xi < 3; xi++ )
					for( int yi = 0; yi < 3; yi++ ) {
						a += ColorIntUtil.getAValue( big.colorAt( x*3 + xi, y*3 + yi ) );
						r += ColorIntUtil.getRValue( big.colorAt( x*3 + xi, y*3 + yi ) );
						g += ColorIntUtil.getGValue( big.colorAt( x*3 + xi, y*3 + yi ) );
						b += ColorIntUtil.getBValue( big.colorAt( x*3 + xi, y*3 + yi ) );
					}
				
				a /= 9; r /= 9; g /= 9; b /= 9;
				out.setPixel( ColorIntUtil.getIntColor( a, r, g, b ), x, y );		
			}
		
	}

	private DownSample() {
	}
	
	
}
